<template>
  <el-card class="box-card">
    <div slot="header">
      <el-steps :active="activeStep">
        <el-step
          v-for="(step, i) of steps"
          :title="step.title"
          :icon="step.icon"
          :key="step.title"
          @click.native="activeStep = i">
        </el-step>
      </el-steps>
    </div>

    <div>
      <el-form ref="form" :model="course" label-width="80px">
        <!--        基本信息      -->
        <div v-show="activeStep === 0">
          <el-form-item label="课程名称">
            <el-input v-model="course.courseName"></el-input>
          </el-form-item>
          <el-form-item label="课程简介">
            <el-input v-model="course.brief"></el-input>
          </el-form-item>
          <el-form-item label="课程概述">
            <el-input v-model="course.previewFirstField"></el-input>
          </el-form-item>
          <el-form-item label="讲师姓名">
            <el-input v-model="course.teacherDTO.teacherName"></el-input>
          </el-form-item>
          <el-form-item label="讲师简介">
            <el-input v-model="course.teacherDTO.description"></el-input>
          </el-form-item>
          <el-form-item label="课程排序">
            <el-input-number
              v-model="course.sortNum"
              controls-position="right"
              :min="0">
            </el-input-number>
          </el-form-item>
        </div>

        <!--        课程封面      -->
        <div v-show="activeStep === 1">
          <el-form-item label="课程封面">
            <course-image v-model="course.courseListImg"></course-image>
          </el-form-item>
          <el-form-item label="解锁封面">
            <course-image v-model="course.courseImgUrl"></course-image>
          </el-form-item>
        </div>

        <!--        销售信息      -->
        <div v-show="activeStep === 2">
          <el-form-item label="售卖价格">
            <el-input v-model="course.discounts" type="number">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="商品原价">
            <el-input v-model="course.price" type="number">
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="销量">
            <el-input v-model="course.sales" type="number">
              <template slot="append">单</template>
            </el-input>
          </el-form-item>
          <el-form-item label="活动标签">
            <el-input v-model="course.discountsTag"></el-input>
          </el-form-item>
        </div>

        <!--        秒杀信息    -->
        <div v-show="activeStep === 3">
          <el-form-item label="限时秒杀开关" label-width="120px">
            <el-switch
              v-model="course.activityCourse"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
          <div v-show="course.activityCourse">
            <el-form-item label="开始时间">
              <el-date-picker
                v-model="course.activityCourseDTO.beginTime"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间">
              <el-date-picker
                v-model="course.activityCourseDTO.endTime"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="秒杀价">
              <el-input v-model="course.activityCourseDTO.amount">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item label="库存">
              <el-input v-model="course.activityCourseDTO.stock">
                <template slot="append">个</template>
              </el-input>
            </el-form-item>
          </div>
        </div>

        <!--        课程详情     -->
        <div v-show="activeStep === 4">
          <el-form-item label="课程详情">
            <text-editor
              v-model="course.courseDescriptionMarkDown"
              :data-loaded="dataLoaded"
            ></text-editor>
          </el-form-item>
        </div>
      </el-form>

      <el-button
        v-if="activeStep < steps.length - 1"
        style="margin-top: 12px;"
        @click="next"
      >
        下一步
      </el-button>
      <el-button
        v-else
        type="primary"
        @click="onSubmit"
      >
        保存
      </el-button>
    </div>
  </el-card>
</template>

<script>
  import { getCourseById, saveOrUpdateCourse } from '@/services/course'
  import CourseImage from './CourseImage'
  import TextEditor from '@/components/TextEditor'

  export default {
    name: 'CreateOrEditCourse',
    props: {
      courseId: {
        type: [Number, String]
      }
    },
    data() {
      return {
        steps: [{
          title: '基本信息',
          icon: 'el-icon-edit',
        }, {
          title: '课程封面',
          icon: 'el-icon-upload',
        }, {
          title: '销售信息',
          icon: 'el-icon-picture',
        }, {
          title: '秒杀信息',
          icon: 'el-icon-picture',
        }, {
          title: '课程详情',
          icon: 'el-icon-picture',
        }],
        activeStep: 0,
        dataLoaded: false,
        course: {
          // 课程名称
          courseName: '',
          // 课程介绍
          brief: '',
          teacherDTO: {
            // 讲师名称
            teacherName: '',
            teacherHeadPicUrl: '',
            position: '',
            // 讲师介绍
            description: ''
          },
          // 课程详情内容
          courseDescriptionMarkDown: '<h3>默认值</h3>',
          // 商品原价
          price: 0,
          // 售卖价格
          discounts: 0,
          priceTag: '',
          // 活动标签
          discountsTag: '',
          isNew: true,
          isNewDes: '',
          // 课程封面地址
          courseListImg: '',
          // 解锁封面地址
          courseImgUrl: '',
          // 课程排序
          sortNum: 0,
          // 概述1
          previewFirstField: '',
          // 概述2
          previewSecondField: '',
          // 上架状态，默认值 0 代表不上架，1 代表上架
          status: 0,
          // 销量
          sales: 0,
          // 参与秒杀活动的课程
          activityCourse: false,
          activityCourseDTO: {
            // 秒杀活动开始时间
            beginTime: '',
            // 结束时间
            endTime: '',
            // 秒杀活动价格
            amount: 0,
            // 秒杀库存
            stock: 0
          },
          autoOnlineTime: ''
        }
      }
    },
    created() {
      this.loadCourse()
    },
    methods: {
      next() {
        this.activeStep++
      },
      async loadCourse() {
        if (this.courseId) {
          const { code, data } = await getCourseById(this.courseId)
          if (code === '000000') {
            this.course = data
            if (!this.course.activityCourseDTO) {
              this.course.activityCourseDTO = {}
            }
            this.dataLoaded = true
          } else {
            this.$message.error('获取课程数据失败')
          }
        }
      },
      async onSubmit() {
        const { code } = await saveOrUpdateCourse(this.course)
        if (code === '000000') {
          this.$message.success(`${this.courseId ? '修改' : '添加'}成功`)
          this.$router.push({ name: 'course' })
        }
      },
    },
    components: {
      CourseImage, TextEditor,
    }
  }
</script>
